// osgMeasureMultiLineEvent.cpp : 定义 DLL 应用程序的导出函数。
//

#include "osgMeasureMultiLineEvent.h"
#include "Windows.h"
#include "IUserEventCallBack.h"

COsgMeasureMultiLineEvent::COsgMeasureMultiLineEvent(osgViewer::View *view, osg::Group *root, IEventCall *iCall)
    : CBaseUserEvent(view),
      mView(view),
      mRoot(root),
      miCall(iCall),
      isActivated(false)
{
}

bool COsgMeasureMultiLineEvent::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
	if (isActivated)
	{
		if (IsLeftClick(ea) && GetKeyState(VK_CONTROL) < 0)
		{
            CPickResult pickResult = PickResult(ea.getX(), ea.getY(), mView);
			osg::Vec3d worldPt = pickResult.worldPt;

			if (worldPt.x() == 0 && worldPt.y() == 0 && worldPt.z() == 0)
			{
				return false;
			}

			vecWorldCoord.push_back(worldPt);

			//计算距离
			double length = CalLength(vecWorldCoord);

            //发送消息 !
            char strMsg[99];
            sprintf_s(strMsg, "长度%lf", length);
            miCall->notifyMeasureMsg(strMsg);
		}
		else if (IsMouseMove(ea) && GetKeyState(VK_CONTROL) < 0)
		{
            CPickResult pickResult = PickResult(ea.getX(), ea.getY(), mView);
			osg::Vec3d worldPt = pickResult.worldPt;

			if (worldPt.x() == 0 && worldPt.y() == 0 && worldPt.z() == 0)
			{
				return false;
			}

			//计算距离
			int totalNum = vecWorldCoord.size();

			if (totalNum == 0)
			{
				return false;
			}

			double length = CalLength(vecWorldCoord);
			length += CalculateDist(worldPt, vecWorldCoord[totalNum - 1]);

            //发送消息 !
            char strMsg[99];
            sprintf_s(strMsg, "长度%lf", length);
            miCall->notifyMeasureMsg(strMsg);
		}
		else if (IsDoubleClick(ea))
		{
            CPickResult pickResult = PickResult(ea.getX(), ea.getY(), mView);
			osg::Vec3d worldPt = pickResult.worldPt;

			if (worldPt.x() == 0 && worldPt.y() == 0 && worldPt.z() == 0)
			{
				return false;
			}

			//计算距离
			int totalNum = vecWorldCoord.size();

			if (totalNum == 0)
			{
				return false;
			}

			double length = CalLength(vecWorldCoord);
			length += CalculateDist(worldPt, vecWorldCoord[totalNum - 1]);

            //发送消息 !
            char strMsg[99];
            sprintf_s(strMsg, " 长度 %lf", length);
            miCall->notifyMeasureMsg(strMsg);

			//发送消息
			vecWorldCoord.clear();
		}
		else if(IsKeyPress(ea))
		{
			if (ea.getKey() == osgGA::GUIEventAdapter::KEY_BackSpace)
			{
                //发送消息 !
                char strMsg[99] = "";
                miCall->notifyMeasureMsg(strMsg);
				vecWorldCoord.pop_back();
			}
			else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Z)					 //z
			{
                char strMsg[99] = "";
                miCall->notifyMeasureMsg(strMsg);
				vecWorldCoord.clear();
			}
		}
	}

	return false;
}

void COsgMeasureMultiLineEvent::updateActivatedStatus(std::vector<OperatingState> vecOpState)
{
    bool flag = false;

    if (isActivated == true)
    {
        flag = true;
    }

    isActivated = isActivate(vecOpState, OPS_MEASURE_MULTIlINE);

	if (isActivated == false && flag == true)
	{
	}

	if (isActivated && flag == false)																//刚激活
	{
	}
}
